package com.itheima.controller;


import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Feedback;
import com.itheima.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 反馈表 前端控制器
 * </p>
 *
 * @author wangchang
 * @since 2020-08-04
 */
@Controller
@RequestMapping("/feedback")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    // 获取反馈列表
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> searchMap) {
        Page<Feedback> pageList = feedbackService.getPageList(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent((Integer) searchMap.get("currentPage"));
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());

        return new Result("success", result);
    }

    // 编辑反馈
    @PostMapping("/edit")
    public Result edit(@RequestBody Feedback feedback) {
        try {
            feedbackService.add(feedback);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "编辑失败");
        }
        return new Result("success", "编辑成功");
    }

    // 批量删除反馈
    @PostMapping("/deleteBatch")
    public Result deleteBatch(List<Feedback> feedbackList) {
        try {
            feedbackService.deleteBatch(feedbackList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "删除失败");
        }
        return new Result("success", "删除成功");

    }

}

